package com.study.web.controller.map;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.study.common.core.controller.AppBaseController;
import com.study.common.core.domain.R;
import com.study.map.dto.GeoResponse;
import com.study.map.dto.ReGeoResponse;
import com.study.map.service.IGeoService;
import com.study.map.service.IReGeoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 地图服务演示控制器
 *
 * @author study
 * @date 2025-08-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/map")
public class MapController extends AppBaseController {

    private final IGeoService geoService;

    private final IReGeoService reGeoService;

    /**
     * 地理编码：将地址转换为经纬度坐标
     */
    @SaCheckLogin
    @GetMapping("/geo")
    public R<GeoResponse> geoCode(
            @RequestParam String address,
            @RequestParam(required = false) String city) {
        try {
            GeoResponse response = geoService.geoCode(address, city);
            return R.ok(response);
        } catch (Exception e) {
            return R.fail("地理编码失败: " + e.getMessage());
        }
    }

    /**
     * 逆地理编码：将经纬度坐标转换为地址
     */
    @SaCheckLogin
    @GetMapping("/regeo")
    public R<ReGeoResponse> reGeoCode(
            @RequestParam String longitude,
            @RequestParam String latitude) {
        try {
            ReGeoResponse response = reGeoService.reGeoCode(longitude, latitude);
            return R.ok(response);
        } catch (Exception e) {
            return R.fail("逆地理编码失败: " + e.getMessage());
        }
    }
}